package com.spatio.serving.basic.service.template.core.component.retry;

import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.spatio.serving.basic.service.template.common.dto.RetryDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class RetryConfig {
    @Value("${spatio.retry.sleepTime:10}")
    private long sleepTime;
    @Value("${spatio.retry.attemptNumber:3}")
    private int attemptNumber;

    @Bean("activityDrawRetry")
    public Retryer<RetryDto> landmarkRetry() {
        return RetryerBuilder.<RetryDto>newBuilder()
                //retry with result condition
                .retryIfResult(retryDto -> retryDto != null && retryDto.getRetry() != null && retryDto.getRetry())
                //wait sleep time
                .withWaitStrategy(WaitStrategies.fixedWait(sleepTime, TimeUnit.MILLISECONDS))
                //max retry number
                .withStopStrategy(StopStrategies.stopAfterAttempt(attemptNumber))
                .build();
    }


}
